package com.example.recyclerviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.example.recyclerviewtest.databinding.ActivityMainBinding;
import com.example.recyclerviewtest.databinding.ActivityMainBindingImpl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Toolbar toolbar = binding.toolbar;

        //初始化ViewPager2
        ViewPager2 viewPager = binding.vpWarehouse;
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), getLifecycle()));

        binding.bootomNavigation.setOnItemSelectedListener(item -> {
            if (item.getItemId() ==  R.id.nav_home) {
                viewPager.setCurrentItem(0);
                toolbar.setTitle("首页");
            }else  if (item.getItemId() ==  R.id.nav_dashboard) {
                viewPager.setCurrentItem(1);
                toolbar.setTitle("学习");
            }else if (item.getItemId() ==  R.id.nav_notifications) {
                viewPager.setCurrentItem(2);
                toolbar.setTitle("社区");
            }else if (item.getItemId() ==  R.id.nav_settings) {
                viewPager.setCurrentItem(3);
                toolbar.setTitle("关于");
            }
            return true;
        });
    }

    private static class FragmentAdapter extends FragmentStateAdapter {
        public FragmentAdapter(androidx.fragment.app.FragmentManager fragmentManager, Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    //return HomeFragment.newInstance("param1", "param2");
                case 1:
                    //return DashboardFragment.newInstance("param1", "param2");
                default:
                    return new HomeFragment();
            }
        }

        @Override
        public int getItemCount() {
            return 4;
        }
    }

}